<template>
  <div class="app-container">
    <!-- 搜索部分-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="申请人" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮部分-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:report:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:report:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:report:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:report:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 列表部分-->
    <el-table :border="true" v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column label="申请人" align="center" prop="username" v-if="columns[0].visible"/>
      <el-table-column label="姓名" align="center" prop="name" v-if="columns[1].visible"/>
      <el-table-column label="报告类型" align="center" prop="reportType" v-if="columns[2].visible"/>
      <el-table-column label="报告内容" align="center" prop="jobContent" v-if="columns[3].visible"/>
      <el-table-column label="与其他部门对接情况" align="center" prop="departmentDocking" v-if="columns[4].visible"/>
      <el-table-column label="分公司公司对接情况" align="center" prop="companyDocking" v-if="columns[5].visible"/>
      <!--  <el-table-column label="所属租户" align="center" prop="tenantId"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:report:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:report:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器部分-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工工作报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="false" label-width="200px">
        <el-form-item label="申请人" prop="username">
          <el-input v-model="form.username" placeholder="请输入申请人" style="width: 200px"/>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
<!--          <Elselect :list="staffList" @fous="fousfn" v-model="form.name" style="width: 200px"></Elselect>-->
          <el-input style="width: 200px" v-model="form.name" readonly placeholder="请选择员工">
            <template slot="append">
              <el-button type="primary" @click="openImportUser2">
                选择员工
              </el-button>
            </template>
          </el-input>

        </el-form-item>

        <!--        <el-form-item label="删除标识" prop="delFlag">-->
        <!--          <el-input disabled v-model="form.delFlag" style="width: 200px"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="报告类型" prop="reportType">
          <el-select v-model="form.reportType" placeholder="请选择报告类型" style="width: 200px">
            <el-option v-for="item in reportTypeOptions" :key="item.dictValue" :label="item.dictLabel"
                       :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="与其他部门对接情况" prop="departmentDocking">
          <el-input type="textarea"
                    maxlength="400"
                    show-word-limit
                    v-model="form.departmentDocking"
                    placeholder="请输入内容"
                    style="width: 400px"/>
        </el-form-item>
        <el-form-item label="分公司公司对接情况" prop="companyDocking">
          <el-input type="textarea"
                    maxlength="400"
                    show-word-limit
                    v-model="form.companyDocking"
                    placeholder="请输入内容"
                    style="width: 400px"/>
        </el-form-item>
        <el-form-item label="工作内容" prop="jobContent">
          <editor v-model="form.jobContent" :min-height="192" style="width: 400px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <Elselect @meiye="myfn" @anme="anmefn" @chongzhi="czfn" @serch="serchfn" @dangqian="dqfn" ref="import" :list="staffList" ></Elselect>

  </div>
</template>

<script>
import {listReport, getReport, delReport, addReport, updateReport} from "@/api/module/report";
import {getDicts} from '@/api/system/dict/data'
import {listStaff} from "@/api/module/staff";
import Elselect from "@/components/ELselect/Elselect"

export default {
  name: "Report",
  components: {
    Elselect,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工工作报告表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        username: null,//申请人
        staffId: null,
        name: null,//姓名
        reportType: null,//报告类型（1日报 2周报 3月报 4年报）
        jobContent: null,//工作内容
        departmentDocking: null,//与其他部门对接情况
        companyDocking: null,//分公司公司对接情况
        tenantId: null//所属租户
      },
      // 表单参数
      form: {},
      columns: [
        { key: 0, label: `申请人`, visible: true },
        { key: 1, label: `员工名`, visible: true },
        { key: 2, label: `报告类型`, visible: true },
        { key: 3, label: `报告内容`, visible: true },
        { key: 4, label: `与其他部门对接情况`, visible: true },
        { key: 5, label: `分公司对接情况`, visible: true },


      ],
      // 表单校验
      rules: {
        userId: [{required: true, message: "请输入用户ID", trigger: "blur"}],
        username: [{required: true, message: "请输入申请人", trigger: "blur"}],
        name: [{required: true, message: "请输入姓名", trigger: "blur"}],
        staffId: [{required: true, message: "请输入员工ID", trigger: "blur"}],
        reportType: [{required: true, message: "请选择日报类型", trigger: "change"}],
        jobContent: [{required: true, message: "请输入工作内容", trigger: "blur"}],
        departmentDocking: [{required: true, message: "请输入与其他部门对接情况", trigger: "blur"}],
        companyDocking: [{required: true, message: "请输入与分公司公司对接情况", trigger: "blur"}],
      },
      reportTypeOptions: [],//报告类型数组
      staffList:[],// 名字组件传参
    };
  },
  created() {
    this.getList();
    this.getReportType()
  },
  methods: {
    //1.获取报告类型
    getReportType() {
      getDicts('report_type').then(res => {
        console.log(res)
        this.reportTypeOptions = res.data
      })
    },
    fousfn(val){
      this.form.name=val
    },

    /** 查询员工工作报告列表 */
    getList() {
      this.loading = true;
      listReport(this.queryParams).then(response => {
        console.log('工作报告', response.rows)//[] 有工作内容 response.jobContent
        response.rows.forEach(item => {
          console.log(item.jobContent)
        })
        this.reportList = response.rows;
        //报告类型的处理
        let arr = this.reportList
        arr.forEach(item => {
          item.reportType = this.getValue(this.reportTypeOptions, item.reportType)
        })
        this.reportList = arr
        this.total = response.total;
        this.loading = false;
      });
    },
    //回显处理
    getValue(arr, item) {
      for (let i = 0; i < arr.length; i++) {
        if (item === arr[i].dictValue) {
          return arr[i].dictLabel
        }
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        username: null,
        staffId: null,
        name: null,
        reportType: null,
        jobContent: null,
        departmentDocking: null,
        companyDocking: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null,
        delFlag: null,
        tenantId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
  async  handleAdd() {
      this.reset();
      this.form.delFlag = 0//0代表存在 2代表删除
      this.form.username = this.$store.getters.name
      this.form.userId=this.$store.getters.id
      const res = await listStaff(this.queryParams)
      console.log('查看----',res)
      this.staffList=res.rows
      this.open = true;
      this.title = "添加员工工作报告";
      this.$refs.import.unit =res.total
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工工作报告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.form.jobContent = this.form.jobContent.replace(/<.*?>/ig, "")
          if (this.form.id != null) {
            updateReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工工作报告编号为"' + ids + '"的数据项？').then(function () {
        return delReport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    },
    openImportUser2() {
      this.$refs.import.show();
    },
    async myfn(val){
      console.log(val)
      this.queryParams.pageSize=val
      const res = await listStaff(this.queryParams)
      // console.log(res.rows)
      this.staffList=res.rows
    },
    async dqfn(val){
      this.queryParams.pageNum=val
      const res = await listStaff(this.queryParams)
      // console.log(res.rows)
      this.staffList=res.rows
    },
    //组件搜索功能
    async serchfn(val){
      this.queryParams.name=val
      const res = await listStaff(this.queryParams)
      this.staffList=res.rows
    },
    //组件重置功能
    async czfn(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        username: null,
        trainingItems: null,
        sponsor: null,
        sponsorDept: null,
        participant: null,
        participantDept: null,
        initiateTime: null,
        startTime: null,
        endTime: null,
        days: null,
        status: null,
        tenantId: null,
        staffId: null,
        name: null,
      }
      const res = await listStaff(this.queryParams)
      this.staffList=res.rows
      this.listDatafn()
    },
    anmefn(val){

      this.form.name=val.name
      this.form.staffId=val.id
      this.$refs.import.visible=false
    }
  }
};
</script>
